package handler

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"right/api/request"
	"right/internaml/dao"
	"right/internaml/model"
	"right/pkg"
)

func AddUser(c *gin.Context) {
	var req request.AddUser

	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "取值失败",
			"data":     err.Error(),
		})
		return
	}
	data := model.User{
		Username: req.Username,
		Password: req.Password,
	}

	err := dao.AddUser(data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "注册失败",
			"data":     nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "注册成功",
		"data":     data,
	})

}

func LoginUser(c *gin.Context) {
	var req request.LoginUser

	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "取值失败",
			"data":     err.Error(),
		})
		return
	}
	data := model.User{
		Username: req.Username,
		Password: req.Password,
	}
	dao.QueryByTable(data, &data)
	token, err := pkg.NewJWT("2210a").CreateToken(pkg.CustomClaims{
		ID: data.Id,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "token失败",
			"data":     err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "取值失败",
		"data":     token,
	})
}

func AddBoo(c *gin.Context) {
	var req request.AddBoo
	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "取值失败",
			"data":     err.Error(),
		})
		return
	}
	data := model.Boo{
		BooId:    req.BooId,
		UserId:   req.UserId,
		Username: req.Username,
		Sex:      req.Sex,
	}

	if !dao.Create(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "注册失败",
			"data":     nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "注册成功",
		"data":     data,
	})

}

func Img(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "./status/img" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	if !pkg.UploadImg(file.Filename) {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "注册失败",
			"data":     nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "注册成功",
		"data":     file.Filename,
	})

}

func AddBooF(c *gin.Context) {
	var req request.AddBooF
	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "取值失败",
			"data":     err.Error(),
		})
		return
	}
	data := model.BooF{
		User:  req.User,
		CType: req.CType,
	}

	for i := 0; i < 1000; i++ {
		if !dao.Create(&data) {
			c.JSON(http.StatusOK, gin.H{
				"code":     1000,
				"message ": "注册失败",
				"data":     nil,
			})
			return
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "注册成功",
		"data":     data,
	})

}
func QueryAll(c *gin.Context) {
	date := model.Boo{}

	if !dao.QueryByTable(date, &date) {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "查询失败",
			"data":     nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "查询成功",
		"data":     date,
	})
}
func UpdateBoo(c *gin.Context) {
	var req request.UpdateBoo
	if err := c.ShouldBind(req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "取值失败",
			"data":     err.Error(),
		})
		return
	}
	data := model.Boo{
		Iid:  req.Iid,
		CTyp: req.CTyp,
	}

	if !dao.Update(data, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":     1000,
			"message ": "修改失败",
			"data":     nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":     0,
		"message ": "修改成功",
		"data":     data,
	})

}
